const expres=require('express')
const utils=require('utility')

const fs=require('fs')
const path=require('path')

const Router=expres.Router()
const model=require('./model')



const TokenTable=model.getModel('token_table')
const ProductTable=model.getModel('product_table')




Router.get('/list',(req,res)=>{
  const token=req.header('token')
  TokenTable.findOne({token},function(e,d){
    if(!d){
      return res.json({code:1,msg:'token已失效'})
    }

    ProductTable.find({},function(e1,d1){
      return res.json({code:0, data:d1})
    })
  })
})

Router.post('/create',(req,res)=>{
  const {name,kind_id,des,pic}=req.body

  const token=req.header('token')

  TokenTable.findOne({token},function(e,d){
    if(!d){
      return res.json({code:1,msg:'token已失效'})
    }

    const productTable=new ProductTable({name,kind_id,des,pic})
    productTable.save((e,d)=>{
      if(e){
        return res.json({code:1,msg:'后端出错了'})
      }
      return res.json({code:0,data:{d}})
    })
  })
})

Router.post('/delete',(req,res)=>{
  const {_id}=req.body
  const token=req.header('token')

  TokenTable.findOne({token},function(e,d){
    if(!d){
      return res.json({code:1,msg:'token已失效'})
    }
    ProductTable.remove({_id},(err)=>{
      if(err){
        return res.json({code:1,msg:'删除失败'})
      }
      return res.json({code:0,msg:'删除成功'})
    })
  })
})

Router.post('/edit',(req,res)=>{
  const {_id,name,kind_id,des,pic}=req.body

  const token=req.header('token')

  TokenTable.findOne({token},function(e,d){
    if(!d){
      return res.json({code:1,msg:'token已失效'})
    }
    ProductTable.findOneAndUpdate({_id}
      ,{name,kind_id,des,pic}
      ,function(e1,d1){
        if(e1){
          return res.json({code:1,msg:'后端出错了'})
        }
        return res.json({code:0,data:d1})
    })
  })
})

Router.post('/upload',(req,res)=>{
  console.log(req.query);
  const {token}=req.query

  TokenTable.findOne({token},function(e,d){
    if(!d){
      return res.json({code:1,msg:'token已失效'})
    }

    const file=req.files['pic']
    const fullName=file.name
    const newName=Date.now()+"-"+fullName;
    const fullNameArray=fullName.split('.')
    const extraName=fullNameArray[fullNameArray.length-1]
    if(extraName!=='png'&&extraName!=='jpg'){
      return res.json({code:1,msg:"只接收png或者jpg格式"})
    }
    file.mv(`${__dirname}/public/images/${newName}`,(err)=>{
      if(err){
        return res.json({code:1,msg:"上传失败"})
      }
      return res.json({code:0,data:{fileName:newName}})
    })
  })
})


Router.get('/images',(req, res)=>{
  const {token,pic}=req.query
  TokenTable.findOne({token},function(e,d){
    if(!d){
      return res.json({code:1,msg:'token已失效'})
    }

    const fileName=path.resolve(__dirname
      ,`/Test/react/exercise/coco-tea/server/public/images/${pic}`)
    res.sendFile(fileName)
    //读取文件内容
    fs.readFile(fileName,(err,data)=>{
      console.log(data);
      if(err){
        console.error(err)
        return
      }

      //data是二进制类型，需要转换为字符串
      console.log(data)
    })
  })



})

module.exports=Router
